package com.sunny.ynhz.ui.patrol.problems.draft;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.ProblemDraftAdapter;
import com.sunny.ynhz.bean.ProBean;
import com.sunny.ynhz.db.RealmUtils;
import com.sunny.ynhz.ui.base.BaseFragment;
import com.sunny.ynhz.utils.SPUtil;

import java.util.LinkedList;

import butterknife.BindView;
import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmResults;

/**
 * Created by luao on 2017/7/24.
 */

public class ProblemDraftFragment extends BaseFragment {
    @BindView(R.id.problem_draft_recycle)
    RecyclerView problemDraftRecycle;
    private ProblemDraftAdapter problemDraftAdapter;
    private LinkedList<ProBean> dataBeenList;
    private Realm mRealm;


    public static ProblemDraftFragment getInstance() {
        return new ProblemDraftFragment();
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_problem_draft;
    }


    @Override
    protected void init() {
        super.init();
        dataBeenList = new LinkedList<>();

        mRealm = RealmUtils.getDeleteRealm();
        final RealmResults<ProBean> probelmList = mRealm
                .where(ProBean.class)
                .equalTo("userId", SPUtil.get("userId", "null").toString())
                .findAllAsync();


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
        problemDraftRecycle.setLayoutManager(linearLayoutManager);
        problemDraftAdapter = new ProblemDraftAdapter(probelmList);
        problemDraftRecycle.setAdapter(problemDraftAdapter);

        probelmList.addChangeListener(new RealmChangeListener<RealmResults<ProBean>>() {
            @Override
            public void onChange(RealmResults<ProBean> proBeen) {
                problemDraftAdapter.notifyDataSetChanged();
            }
        });



//        probelmList.addChangeListener(new RealmChangeListener<RealmResults<ProBean>>() {
//            @Override
//            public void onChange(RealmResults<ProBean> proBeen) {
//                dataBeenList.addAll(probelmList);
//                problemDraftAdapter.notifyDataSetChanged();
//            }
//        });
//
        problemDraftAdapter.setOnItemClickListener(new ProblemDraftAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, ProBean itemData, int position) {
                Intent intent = new Intent(activity, ProblemDraftDetailActivity.class);
                intent.putExtra("id", itemData.getId());
                startActivityForResult(intent, 100);
            }
        });
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

//        patrolProblemAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mRealm != null) {
            mRealm.close();
        }
    }
}
